from kafka import KafkaProducer

if __name__ == '__main__':
    # 创建生产者
    producer = KafkaProducer(bootstrap_servers="master:9092,node2:9092,node2:9092")

    # 读取文件
    with open("../../data/cars_sample.json", mode="r", encoding="UTF-8") as file:
        cars = [line.strip() for line in file.readlines()]

    # 循环将数据写入kafka
    for car in cars:
        producer.send(topic="car", value=car.encode("UTF-8"))

    # 关闭连接
    producer.close()
